﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Data;
using System.Web.UI.WebControls;
using Thksoft.Modal.Page;

namespace Thksoft.Modal
{
    public partial class EditGeneralStatistical : FormPage
    {
        private QS_PlanDetail_DropLIstBLL dropBLL = new QS_PlanDetail_DropLIstBLL();

        protected override void BindPage()
        {
            BindCompany();
            BindType();
            BindProject();
            if (FORM_IFEDIT)
            {
                var dropListType = Request.QueryString["TYPE"];

                var entity = dropBLL.FindPlanDropList(Convert.ToDecimal(FORM_KEY));
                FORM_NAME.Text = entity.C_NAME;
                FORM_DropListType.Text = dropListType;
                FORM_CMY.Text = entity.N_CMYID.ToString();
                if (entity.C_TABLETYPE == "T1")
                {
                    BindProject();
                    FORM_PRIVATE.Visible = true;
                    FORM_PRIVATE.SelectedValue = entity.N_PRIVATE.ToString();
                }
            }
            else
            {
                FORM_NAME.Text = "";
                FORM_DropListType.Text = "-1";
                FORM_CMY.Text = CurrentUserCompanyID.ToString();
                FORM_PRIVATE.Text = "-1";
            }

            //if (lognUser.USER_COMPANY == 1)
            //{
            //    FORM_CMY.Enabled = true;
            //}
            //else
            //{
            //    FORM_CMY.Enabled = false;
            //}
        }

        protected override void SaveClick(object sender, EventArgs e)
        {
            string tableType = Request.QueryString["FundType"].ToString();

            if (FORM_NAME.Text.TrimEnd() == "")
            {
                MessageBox.Show(this, "项目名称不能为空");
                return;
            }
            if (FORM_DropListType.Text.TrimEnd() == "-1")
            {
                MessageBox.Show(this, "所属种类不能为空");
                return;
            }
            var entity = new QS_PlanDetail_DropLIst();
            entity.N_ID = 1;
            entity.C_NAME = FORM_NAME.Text.Trim();
            entity.C_TABLETYPE = tableType;
            entity.N_NAMETYPE = Convert.ToDecimal(FORM_DropListType.Text);
            entity.N_PRIVATE = Convert.ToDecimal(FORM_PRIVATE.Text == null ? "" : FORM_PRIVATE.Text);
            entity.N_CMYID = Convert.ToDecimal(FORM_CMY.Text == null || FORM_CMY.Text == "-1" ? lognUser.USER_COMPANY.ToString() : FORM_CMY.Text);

            if (FORM_IFEDIT)
            {
                decimal dropListId = Convert.ToDecimal(Request.QueryString["ID"]);
                entity.N_ID = dropListId;
                bool istrue = dropBLL.UpdatePlanDropList(entity, CurrentUserName);
                //var entity4 = dropBLL.FindPlanDropList(Convert.ToDecimal(FORM_KEY));
                if (istrue)
                {
                    MessageBox.Show(this, "修改成功");
                }
                else
                {
                    MessageBox.Show(this, "修改失败");
                    return;
                }
            }
            else
            {
                bool istrue = dropBLL.InsertPlanDropList(entity, CurrentUserName);
                decimal dropId = entity.N_ID;
                if (istrue)
                {
                    MessageBox.ShowAndRedirect(this, "新增成功", "/Modal/EditGeneralStatistical.aspx?FundType=" + tableType + "&IF_EDIT=TRUE&ID=" + dropId);
                }
                else
                {
                    MessageBox.Show(this, "新增失败");
                    return;
                }
            }
        }

        protected override void UpdateTitle()
        {
            if (FORM_IFEDIT)
            {
                MessageBox.ResponseScript(this, "FORM_UPDATE_TITLE(\"编辑资金列表设置\");");
            }
            else
            {
                MessageBox.ResponseScript(this, "FORM_UPDATE_TITLE(\"添加资金列表设置\");");
            }
        }

        /// <summary>
        /// 绑定公司
        /// </summary>
        private void BindCompany()
        {
            DataTable tableOrganization = new Sys_CompanyBLL().SelectByWhere("", lognUser.USER_COMPANY.ToString());
            if (tableOrganization != null && tableOrganization.Rows.Count > 0)
            {
                // setList(tableOrganization, ddlCompany);
                PublicMethod.BindDDL(FORM_CMY, tableOrganization, "CMY_ID", "CMY_NAME", false);

                FORM_CMY.Items.Insert(0, new ListItem("通用", "0"));

                FORM_CMY.SelectedValue = "0";

                FORM_CMY.Visible = true;
            }
            else
            {
                FORM_CMY.Items.Add(new ListItem("无", "-1"));
            }
        }

        /// <summary>
        /// 所属种类
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void FORM_DropListType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (FORM_DropListType.Text == "1")
            {
                FORM_PRIVATE.Visible = true;
            }
            else
            {
                FORM_PRIVATE.Visible = false;
            }
        }

        /// <summary>
        /// 公司
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //protected void FORM_CMY_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    BindProject();
        //    if (FORM_DropListType.Text == "1")
        //    {
        //        FORM_PRIVATE.Visible = true;
        //    }
        //    else
        //    {
        //        FORM_PRIVATE.Visible = false;
        //    }
        //}

        /// <summary>
        ///
        /// </summary>
        private void BindType()
        {
            string tableType = Request.QueryString["FundType"].ToString();
            ListItem li = null;
            if (tableType == "T1")
            {
                li = new ListItem("收入类别", "1");
                FORM_DropListType.Items.Add(li);
            }

            if (tableType == "T2")
            {
                li = new ListItem("用户类别", "2");
                FORM_DropListType.Items.Add(li);
            }

            if (tableType == "T3")
            {
                li = new ListItem("款项类别", "3");
                FORM_DropListType.Items.Add(li);
                li = new ListItem("款项用途", "4");
                FORM_DropListType.Items.Add(li);
            }

            if (tableType == "T4")
            {
                li = new ListItem("款项类别", "3");
                FORM_DropListType.Items.Add(li);
                li = new ListItem("款项用途", "4");
                FORM_DropListType.Items.Add(li);
            }

            if (tableType == "T5")
            {
                li = new ListItem("项目名称", "1");
                FORM_DropListType.Items.Add(li);
                li = new ListItem("款项类别", "3");
                FORM_DropListType.Items.Add(li);
                li = new ListItem("款项用途", "4");
                FORM_DropListType.Items.Add(li);
            }

            if (tableType == "T6")
            {
                li = new ListItem("项目名称", "1");
                FORM_DropListType.Items.Add(li);
                li = new ListItem("款项类别", "3");
                FORM_DropListType.Items.Add(li);
                li = new ListItem("款项用途", "4");
                FORM_DropListType.Items.Add(li);
            }
        }

        /// <summary>
        /// 项目上一级
        /// </summary>
        private decimal pid = 0;

        private void BindProject()
        {
            QS_PROJECTBLL projectbll = new QS_PROJECTBLL();
            decimal cmyid = Convert.ToDecimal(lognUser.USER_COMPANY);
            if (cmyid == -1)
            {
                cmyid = lognUser.USER_COMPANY;
            }
            //var proName = projectbll.Find();
            var project = projectbll.FindByName("经营活动产生的现金流入", cmyid);
            if (project != null)
            {
                pid = project.N_PROID;
            }

            DataTable dt = projectbll.GetInfoByPId(cmyid, pid);
            FORM_PRIVATE.DataSource = dt;
            FORM_PRIVATE.DataTextField = "c_proname";
            FORM_PRIVATE.DataValueField = "n_proid";
            FORM_PRIVATE.DataBind();

            FORM_PRIVATE.Items.Insert(0, new ListItem("-------请选择-------", "-1"));
            //FORM_PRIVATE.SelectedValue = "-1";
            FORM_PRIVATE.Visible = false;
        }
    }
}